package com.bw.merchant.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.bw.common.vm.BaseViewModel
import com.bw.merchant.entity.AddApplicationEntity
import com.bw.merchant.entity.ApprovalDetailEntity
import com.bw.merchant.entity.DetailMerchantEntity
import com.bw.merchant.entity.MerchantListEntityItem
import com.bw.merchant.entity.RecordingEntityItem
import com.bw.merchant.model.MerchantRepository
import com.bw.merchant.page.MerchantDetailActivity
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class MerchantViewModel @Inject constructor(private val repo:MerchantRepository):BaseViewModel() {
    val  mMerchantSuccess = MutableLiveData<List<MerchantListEntityItem>>()
    fun getMerchantData(){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadMerchantData()
            if (res.code == 200){
                mMerchantSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
                ToastUtils.showLong(res.message)
            }
        }
    }
    //详情页面
    val mMerchantDetailSuccess = MutableLiveData<DetailMerchantEntity>()
    fun getMerchantDetailData(id:Int){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadMerchantDetailData(id)
            if (res.code == 200){
                mMerchantDetailSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    //入驻申请记录
    val mRecordingSuccess = MutableLiveData<List<RecordingEntityItem>>()
    fun getRecordingData(){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadRecordingData()
            if (res.code == 200){
                mRecordingSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    //创建商家入驻申请
    val mAddMerchantSuccess = MutableLiveData<AddApplicationEntity>()
    fun getAddMerchantData(addApplicationEntity: AddApplicationEntity){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadAddMerchantData(addApplicationEntity)
            if (res.code==200){
                mAddMerchantSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    //入驻审批 详情
    val mApprovalDetailSuccess = MutableLiveData<ApprovalDetailEntity>()
    fun getApprovalDetailData(id:Int){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadApprovalDetailData(id)
            if (res.code == 200){
                mApprovalDetailSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }


}